<?php
Globals::requireController('Admin');
Globals::requireTable('Goods');
Globals::requireTable('Ordergoods');
Globals::requireTable('User');

class GoodsController extends AdminController
{
    protected $goodsTable;
    protected $ordergoodsTable;
    protected $userTable;
    
	public function __construct($config = null)
	{
		parent::__construct($config, true);
		$this->goodsTable = new GoodsTable($config);
		$this->ordergoodsTable = new OrdergoodsTable($config);
		$this->userTable = new UserTable($config);
	}
	
	public function indexAction()
	{
	    $where = array();
	    $count = $this->goodsTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->goodsTable->listPage($where, 'id ASC', $pageSize, $pageId);
	}
	
	public function addAction()
	{
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $title = $this->goodsTable->getRow($id);
	        if($title['id'] > 0){
	            $this->view->data = $title;
	        }
	    }
	}
	
	public function doaddAction()
	{
	    $id = $this->getIntParam('id');
	    $data['title'] = $this->getParam('title');
	    $data['price'] = $this->getParam('price');
	    $data['repertory'] = $this->getIntParam('repertory');
	    $data['remark'] = $this->getParam('remark');
	    $data['rank'] = $this->getIntParam('rank');
	    $data['activateurl'] = $this->getParam('activateurl');
	    if($id > 0){
	        $this->goodsTable->update($data,$id);
	    }else{
            $data['createtime'] = date('Y-m-d H:i:s');
            $data['status'] = 1;
            $this->goodsTable->add($data);
	    }
	    $this->redirectToAction();
	    exit();
	}
	
	public function deleteAction()
	{
	    $id = $this->getIntParam('id');
	    $this->goodsTable->delete($id);
	    $this->redirectToAction();
	    exit();
	}
	
	public function ordersAction(){
	    $id = $this->getIntParam('id');
	    $data = $this->ordergoodsTable->listAll(array('goods_id'=>$id));
	    $this->view->data = $data;
	}
}
